<?php

namespace app\agent\service;

use app\agent\logic\WkOrderLogic;
use app\agent\servlet\WkOrderServlet;

class WkOrderService extends BaseService
{
    private $wkOrder = null;

    private $servlet = null;

    public function __construct()
    {
        parent::__construct();

        $this->wkOrder = new WkOrderLogic();

        $this->servlet = new WkOrderServlet();

    }

    public function getViewPage($request = [])
    {
        $where = $this->servlet->getWhere($request);

        $data = $this->wkOrder->getPage($where, $request);

        $data = [
            'data'   => $data,
            'status' => $this->wkOrder->_map['status'],
        ];

        return $data;
    }

    /**
     * @action 订单详情
     */
    public function getViewDetail($data = [])
    {
        $where = [
            $this->wkOrder->getPk() => $data['id'],
        ];

        $data = $this->wkOrder->getDetail($where);

        if( $data === false )
        {
            $this->error = $this->wkOrder->getError();

            return false;
        }

        return $data;
    }

    /**
     * @action 订单明细
     */
    public function sellerOrder($request = [])
    {
        $where = [
            ['suid', '=', $request['suid']],
        ];

        //获取当天订单数量
        $where[1]      = ['paytime', 'BETWEEN TIME', [date('Y-m-d', strtotime('now')), date('Y-m-d', strtotime('+1 day'))],];
        $data['today'] = $this->wkOrder->getOrderCount($where);

        //获取昨天订单数量
        $where[1]          = ['paytime', 'BETWEEN TIME', [date('Y-m-d', strtotime('-1 day')), date('Y-m-d', strtotime('now'))],];
        $data['yesterday'] = $this->wkOrder->getOrderCount($where);

        //获取本周订单数量
        $where[1]     = ['paytime', 'BETWEEN TIME', [date('Y-m-d', strtotime('-1 week Monday')), date('Y-m-d', strtotime('now'))],];
        $data['week'] = $this->wkOrder->getOrderCount($where);

        //获取本月订单数量
        $where[1]      = ['paytime', 'BETWEEN TIME', [date('Y-m', time()) . '-01', date('Y-m-d', strtotime('now'))]];
        $data['month'] = $this->wkOrder->getOrderCount($where);

        //获取今年订单数量
        $where[1]     = ['paytime', 'BETWEEN TIME', [date('Y', time()) . '-01-01', date('Y-m-d', strtotime('now'))]];
        $data['year'] = $this->wkOrder->getOrderCount($where);

        return $data;
    }

    /**
     * @action 根据时间获取代理区域的收益
     */
    public function getAgentProfit($request = [])
    {
        $where = $this->servlet->getWhere($request);

        $type    = [1 => 'province', 2 => 'city', 3 => 'county'];
        $where[] = [$type[$request['type']], '=', $request['addr_code']];

        $profit = $this->wkOrder->getAgentProfit($where);

        //代理商收益
//        $data['relFee'] = sprintf("%.2f", $data['fee'] * config('agent.' . $request['type']));//保留两位小数

        return $profit;
    }
}